package buildtools;

import java.util.Collection;
import java.util.Iterator;

import node.GeoNode;

import component.AbstractComponent;
import component.Ball;
import component.Structure;
import component.Tube;

public class Connector {
	
	private Structure structure;

	public Connector(Structure structure) {
		this.structure = structure;
	}

	public void connectToNearbyTubes(Ball ball) {
		
		Collection<GeoNode> geoNodesArray = structure.getPermanentGeoGroup().getGeoNodes();
		Iterator<GeoNode> geoNodesIth = geoNodesArray.iterator();
		
		AbstractComponent component = null;
		
		while (geoNodesIth.hasNext()) {
	
			if ((component = geoNodesIth.next().getComponent()) instanceof Tube) {
				
				if (ball.canConnectTo(component)) ball.connect(component);
			}
		}
	}
	
	public void connectToBall(Tube tube) {
		
		Collection<GeoNode> geoNodesArray = structure.getPermanentGeoGroup().getGeoNodes();
		Iterator<GeoNode> geoNodesIth = geoNodesArray.iterator();
		
		AbstractComponent component = null;
		
		while (geoNodesIth.hasNext()) {
	
			if ((component = geoNodesIth.next().getComponent()) instanceof Ball) {
				if (tube.canConnectTo(component)) tube.connect(component);
			}
		}
	}
	
	public void connectAllPossible() {
		Collection<GeoNode> geoNodeArray = structure.getPermanentGeoGroup().getGeoNodes();
		AbstractComponent component;
		for (GeoNode node : geoNodeArray) {
			component = node.getComponent();
			if (component instanceof Tube) {
				connectToBall((Tube)component);
			}
		}
	}
}
